package com.slz3.business.module.configManagement.conZoneManage.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conZoneManage.domain.ConZoneManage;
import com.slz3.business.module.configManagement.conZoneManage.domain.ConZoneManageFactory;
import com.slz3.business.module.configManagement.conZoneManage.steward.ConZoneManageSpecification;
import com.slz3.business.module.configManagement.conZoneType.domain.ConZoneType;
import com.slz3.business.module.configManagement.conZoneType.domain.ConZoneTypeCondition;
import com.slz3.business.module.configManagement.conZoneType.steward.ConZoneTypeSpecification;
import com.slz3.business.module.goodsManagement.gooGoods.domain.GooGoods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ConZoneManageServiceImpl implements ConZoneManageService {
    @Override
    public ConZoneManage save(ConZoneManage conZoneManage) {
        return Repositorys.conZoneManage.save(ConZoneManageFactory.initBaseFields(conZoneManage));
    }
    @Override
    public Page<ConZoneManage> page(MultiValueMap<String, String> params) {

        Page<ConZoneManage> conZoneManagePage = Repositorys.conZoneManage.findAll(
                ConZoneManageSpecification.findJoin(params), PageableSteward.extract(params)
        );

        // join
        Map<UUID, ConZoneType> joinZoneType = Repositorys.conZoneType.findAllById(
                conZoneManagePage.getContent().stream().map(ConZoneManage::getFkZoneType).collect(Collectors.toList())
        ).stream().collect(Collectors.toMap(ConZoneType::getUuid, Function.identity()));
        // merge
        conZoneManagePage.getContent().stream().forEach(item -> item.setZoneType(joinZoneType.get(item.getFkZoneType())));
        //
        return conZoneManagePage;
    }


    @Override
    public ConZoneManage modify(ConZoneManage conZoneManageNew) {
        return Repositorys.conZoneManage.saveAndFlush(ConZoneManageFactory.initBaseFields(conZoneManageNew));
    }

    @Override
    public Optional<ConZoneManage> single(UUID uuid) {
        return Repositorys.conZoneManage.findById(uuid);
    }

    @Override
    public List<ConZoneManage> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conZoneManage.findAllById(uuids);
    }

    @Override
    public List<ConZoneManage> delete(List<ConZoneManage> conZoneManageList) {
        conZoneManageList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConZoneManageFactory.initBaseFields(item);
        });
        return Services.conZoneManageTransactional.batchModify(conZoneManageList);
    }

}
